<?php
namespace ZHMVC\DB\MANAGER;
if (! isset($_SESSION)) {
	session_start();
}
include (dirname(dirname(__FILE__)) . "/zhconfig/Config.php");
include (ZH_PATH . DS . MANAGERFPATH . DS . 'islogin' . ZH);

$isp = new \ZHMVC\D\MANAGER\isPermission();
$isper = $isp->getPermission();
$_curlid = $isp->getCUrl();
$c = new \ZHCONFIG\ZhConfig();
$db_pre = $c->getDbPre();

if($isper==1)
{
	$ErrMsg="对不起，你没有访问该页面的权限";
	echo $ErrMsg;
	exit;
}
elseif($isper==0)
{
	$ErrMsg="对不起，地址错误";
	echo $ErrMsg;
	exit;
}

include (ZH_PATH . DS . MANAGERFPATH . DS . 'top1' . ZH);
?>
<div class="wap-container">
	<nav class="breadcrumb" style="background-color:#fff;padding: 0 24px">
	管理员管理
	<a class="btn btn-success radius f-r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a>
	</nav>
<?php 
$action=SafeRequest(getPGC('atcion'),0);
switch ($action)
{
    case "save_admin":
        save_admin($db_pre);
        break;
    case "add":
        add($db_pre);
        break;
    case "del":
        del($db_pre);
        break;
    case "power":
        savepower($db_pre);
        break;
    case "edit":
        editpower($db_pre);
        break;
    default:
        main($db_pre);
}

function main($db_pre)
{ 
?>
<article class="Hui-admin-content clearfix">

			<div class="panel mt-20">
				<div class="panel-body">
					<div class="clearfix">
						<span class="f-l">
							<a href="javascript:;" onClick="location='?atcion=add';" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加用户</a>
						</span>
					</div>
					<div class="clearfix mt-20">

            <table class="table table-border table-bordered table-hover table-bg table-sort">
              <thead>
                <tr class="text-c">
        			<th><input type="checkbox" name="" value="">ID</th>
        			<th>登陆名</th>
        			<th>上次登陆时间</th>
        			<th>上次登陆IP</th>
        			<th>状态</th>
        			<th>操作</th>
        		</tr>
              </thead>
              <tbody>
	<?php

	$rs=D("" . $db_pre . "master")->getLinkAll("id,username,lastime,lastip,`state`",true);
	$datas = $rs['datas'];
    $rows = $rs['rows'];
    for ($j = 0; $j < $rows; $j ++) {
        $data = $datas[$j];
        if ($data['state'] == 1) {
            $M_state = "正常";
        } else {
            $M_state = "禁止";
        }
        ?>
  		<tr class="text-c">
			<td><input type="checkbox" name="" value=""><?php echo $data['id'];?></td>
			<td><a href="?atcion=add&id=<?php echo $data['id'];?>"
				title="点击编辑"><?php echo $data['username'];?></a></td>
			<td><?php echo $data['lastime'];?></td>
			<td><?php echo $data['lastip'];?></td>
			<td><?php echo $M_state;?></td>
			<td class="taskOptions"><a href="?atcion=edit&id=<?php echo $data['id'];?>">编辑权限</a>
				| <a href="?atcion=del&id=<?php echo $data['id'];?>"
				onclick="{if(confirm('确定删除吗?')){return true;}return false;}">删除</a></td>
		</tr>
        <?php
        }
        ?>
              </tbody>
            </table>
</div>
				</div>
			</div>
		</article>
  
<?php 
}

function add($db_pre)
{
    $postid = SafeRequest(getPGC('id'), 0);

    if (($postid != "") && ($postid != 0)) {
        $bind = array("id" => $postid);
        $rs=D("" . $db_pre . "master")->where($bind)->getLinkOne("username,`state`",true);
        $datas = $rs['datas'];
        $rows = $rs['rows'];
        $BackName = $datas['username'];
        $State = $datas['state'];
    } else {
        $BackName = "";
        $State = "";
        $typeid = "";
    }
?>
<div class="panel">
	<div class="panel-body">
    <form action="?atcion=save_admin&id=<?php echo $postid;?>" id="form1" name="form1" method="post" class="form form-horizontal">
          <!-- 表单 -->
          	<div class="row clearfix">
					<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>后台登陆名称：</label>
					<div class="form-controls col-xs-8 col-sm-9">
						<input type="text" class="input-text" placeholder="用户名" id="backname" name="backname" value="<?php echo $BackName; ?>" />
					</div>
			</div>
			
			<div class="row clearfix">
					<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>后台登陆密码<br />(<font color="red">如只修改用户登陆名而不修改密码请留空</font>)：</label>
					<div class="form-controls col-xs-8 col-sm-9">
						<input type="password" class="input-text" placeholder="用户名" id="password" name="password" value="<?php echo $BackName; ?>" />
					</div>
			</div>

        	<div class="row clearfix">
					<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>登陆状态：</label>
					<div class="form-controls col-xs-8 col-sm-9">
					<div class="radio-box">
					<input name="state" type="radio" value="1" id="state-1"  <?php
					if ($State == "1") { echo "checked";}?> />
							<label for="state-1">是</label>
						</div>
						<div class="radio-box">
							<input type="radio" id="state-2" value="0" name="state"
							<?php
					if ($State == "0") { echo "checked";}?> />
							<label for="state-2">否</label>
						</div>

					</div>
			</div>
        	
			
			
            <div class="row clearfix">
					<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
					<input type="hidden" name="id" value="<?php echo $postid; ?>"><input type="hidden" name="action" value="save"><input type="hidden" name="canshu" value="save">
						<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
					</div>
			</div>
			</form>
          <!-- 表单 -->
          </div>
    	</div>
<?php
}

function editpower($db_pre)
{

    $postid = SafeRequest(getPGC('id'), 0);
    $bind = array("id" => $postid);
    $rs=D("" . $db_pre . "master")->where($bind)->getLinkOne("seting,column_setting",true);
    $datas = $rs['datas'];
    $rows = $rs['rows'];
    $Power = $datas['seting'];
    $Admin_Power = $datas['column_setting'];
?>
<div class="panel">
	<div class="panel-body">
<form action="?atcion=power&id=<?php echo $postid;?>" id="form1" name="form1" method="post" class="form form-horizontal">
<script type="text/javascript">
function selectAll(){
	var checklist = document.getElementsByName ("admin_power[]");

	if(document.getElementById("controlAll").checked)
	{
	   for(var i=0;i<checklist.length;i++)
	   {
	      checklist[i].checked = true;
	      checklist[i].parentNode.setAttribute("class", "checked");
	   }
	}
	else
	{
	  for(var j=0;j<checklist.length;j++)
	  {
		 checklist[j].parentNode.setAttribute("class", "");
		 checklist[j].checked = false;
	  }
	}
}
</script>
<?php
    $bind = array("pid" => 0);
    $rs=D("" . $db_pre . "admin_menu")->where($bind)->getLinkAll("id,title,url",true);
    $datas = $rs['datas'];
    $rows = $rs['rows'];
    //print_r($rs);
    $map=null;
    $m=0;
    for ($i = 0; $i < $rows; $i ++) {
        $data = $datas[$i];
        ?>
<div class="row clearfix">
	<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span><?php echo $data['title']; ?>：</label>
		<div class="form-controls col-xs-8 col-sm-9">
           <?php
        $map['pid']=$data['id'];
        $rs1=D("" . $db_pre . "admin_menu")->where($map)->getLinkAll("id,title,url,menuname",true);
        $datas1 = $rs1['datas'];
        $rows1 = $rs1['rows'];
        $map=null;
        if ($rows1 > 0) {
            for ($j = 0; $j < $rows1; $j ++) {
                $data1 = $datas1[$j];
                echo "<div class=\"skin-minimal\"><div class=\"check-box\"><label><input type=\"checkbox\" id=\"selected".$m."\" name=\"admin_power[]\" value=\"" . $data1['menuname'] . $data1['url'] . "\"";
                if ((substr_count($Power, $data1['menuname'] . $data1['url'] . ",")) > 0) {
                    echo " checked";
                }
                echo ">";
                echo $i . ($j + 1);
                echo "." . $data1['title']."</label></div>
					</div>";
                $m=$m+1;
            }
        }
    ?>
            </div>
        </div>
 <?php }?>
        <div class="row clearfix">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
			<input onclick="selectAll()" type="checkbox" name="controlAll" style="controlAll" id="controlAll"/>全选
			<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			</div>
		</div>
</form>
<!-- 表单 -->
</div>
</div>
<?php
}

function savepower($db_pre)
{
    $Admin_Power = "";
    foreach (getPGC("admin_power") as $j => $b) {
        $Admin_Power = $Admin_Power . $b . ",";
    }
    $Admin_Power = str_replace(" ", "", $Admin_Power);
    $Admin_Power = str_replace(",,", ",", $Admin_Power);
    $postid = SafeRequest(getPGC('id'), 0);
    if (($postid != "") && ($postid != "0")) {
        $wheremap = array("id" => $postid);
        $updatedata = array("seting" => $Admin_Power);
        D("" . $db_pre . "master")->where($wheremap)->LinkUpdate($updatedata);
        
        $rs=D("zhmvc_master")->where($wheremap)->getLinkAll("",true);
        $datas = $rs['datas'];
        $data = $datas[0];
        $_SESSION['Master_Power'] = $data['seting'];//后台权限
 
        echo "<script>alert('更新成功');window.location.href='admin_master.php';</script>";
    }
}

function save_admin($db_pre)
{
    $postid = SafeRequest(getPGC('id'), 0);
    $BackName = SafeRequest(getPGC('backname'), 0);
    $State = SafeRequest(getPGC('state'), 0);
    $PassWord = SafeRequest(getPGC('password'), 0);
    $PSQL = "";
    If ($PassWord != "") {
        $PassWord = Md5($PassWord);
    }
    If (($BackName == "") || (strlen($BackName) > 50)) {
        $ErrMsg = "登陆名为空或长度超过50个字符！";
        echo $ErrMsg;
    }

    if (($postid != "") && ($postid != "0")) {
        if ($PSQL == "") {
            $wheremap = array("id" => $postid);
            $updatedata = array("state" => $State,"username"=>$BackName);
            D("" . $db_pre . "master")->where($wheremap)->LinkUpdate($updatedata);
        } else {
            $wheremap = array("id" => $postid);
            $updatedata = array("state" => $State,"username"=>$BackName,"userpassword"=>$PassWord);
            D("" . $db_pre . "master")->where($wheremap)->LinkUpdate($updatedata);
        }
        echo "<script>alert('更新成功');window.location.href='admin_master.php';</script>";
    } else {
        $column_setting1="";
        $seting1="";
        $bind = array(
            "username" => $BackName
        );
        $rs=D("" . $db_pre . "master")->where($bind)->getLinkAll("id",true);
        $datas = $rs['datas'];
        $rows = $rs['rows'];
        // echo $sql;
        If ($rows != 0) {
            $FoundErr = True;
            $ErrMsg = "对不起，后台登陆名重复！";
        } ElseIf ($PassWord == "") {
            $FoundErr = True;
            $ErrMsg = "对不起，请填写登陆密码！";
        } else {
            $updatedata = array("state" => $State,"username"=>$BackName,"userpassword"=>$PassWord,"column_setting"=>$column_setting1,"seting"=>$seting1);
            D("" . $db_pre . "master")->LinkInsert($updatedata);
            echo "<script>alert('更新成功');window.location.href='admin_master.php';</script>";
        }
        If ($FoundErr) {
            echo $ErrMsg;
        }
    }
}

function del($db_pre)
{
    $postid = SafeRequest(getPGC('id'), 0);
    if (($postid != "") && ($postid != "0")) {
        $wheremap = array("id" => $postid);
        D("" . $db_pre . "master")->where($wheremap)->LinkDelete($wheremap);
        echo "<script>alert('更新成功');window.location.href='admin_master.php';</script>";
    }
}
?>
</div>
<?php 
include (ZH_PATH . DS . MANAGERFPATH . DS . 'foot1' . ZH);
?>
</body>
</html>
